<?php
namespace shenka\controllers;

use Yii;
use yii\web\Controller;
use shenka\controllers\classes\QueryClass;
use yii\db\Query;
use shenka\models\EditionKrDev;
/*
* @author admin 
* @class KadventController
* @desc 星球大冒险（韩）
*/
class KadventController extends Controller {
	/*
	* @author admin 
	* @function Dactive
	* @desc 星球大冒险（韩）激活情况
	*/
	public function actionDactive(){
		$view = Yii::$app->view;
		$view->params['layout_data'] = '星球大冒险（韩）';
		//设备
		$device = QueryClass::dataJson('create_time','%Y-%m-%d','prod_edition_kr_device',['not in','prod_code_id',[20121],15]);
		$dev = json_decode($device);
		$date = $dev->date;

		//产品
		$model = new Query;
    	$query = $model->select(["from_unixtime(create_time,'%Y-%m-%d') as date",'prod_code_id as pro'])->from('prod_edition_kr_device')->where(['not in','prod_code_id',[20121]])->groupBy(['prod_code_id'])->orderBy(['date'=>SORT_DESC])->all();
    	$product = [];
    	//以'日期'=>'产品码'的格式生成新的数组
    	foreach($query as $k){
    		$product[] = $k['date'];
    	}
    	//相同日期元素合并，并显示数量
    	$pro = array_count_values($product);
    	
    	for($i = 0; $i<15; $i++){

    		//xin 判断date是否存在
    		if (!empty($date[$i])) {
    	
	    		if(!in_array($date[$i],array_keys($pro))){
	    			$pro[$date[$i]] = 0;
	    			//var_dump($);exit;
	    		}
    		}
    	}
    	//按原有键名倒序排列数组
    	krsort($pro);
    	//取15条记录
    	$pro = array_chunk($pro,15);
    	krsort($pro[0]);
    	$final = array_values($pro[0]);
		//var_dump(json_encode($final));exit;
		return $this->render('dactive',['d_dev'=>$device, 'd_pro' => json_encode($final)]);
		
	}
	/*
	* @author admin 
	* @function Prodev
	* @desc 星球大冒险（韩）激活设备占比
	*/
	public function actionProdev(){
		$view = Yii::$app->view;
		$view->params['layout_data'] = '星球大冒险（韩）';
		// var_dump($pro);
		$data = EditionKrDev::find()->select(["count(id) as count",'device'])->where(['not in','prod_code_id',[20121]])->groupBy(['device'])->asArray()->all();
    	$pro = QueryClass::getGroupCount($data,'device');
		return $this->render('prodev',['pro'=>json_encode($pro)]);
	}
}